package com.lineage.data.item_etcitem;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.map.L1WorldMap;
import com.lineage.server.serverpackets.S_NPCPack_Pet;
import com.lineage.server.serverpackets.S_NPCPack_Summon;
import com.lineage.server.serverpackets.S_Sound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.Random;

public class New_Whistle extends ItemExecutor {
  private static Random _random = new Random();
  
  public static ItemExecutor get() {
    return new New_Whistle();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    short mapId = pc.getTeleportMapId();
    int head = pc.getTeleportHeading();
    pc.sendPacketsX8((ServerBasePacket)new S_Sound(437));
    if (!pc.isGhost() && pc.getMap().isTakePets()) {
      Object[] petList = pc.getPetList().values().toArray();
      Object[] array;
      int length = (array = petList).length;
      int i = 0;
      while (i < length) {
        Object petObject = array[i];
        if (petObject instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)petObject;
          Iterator<L1NpcInstance> iterator = pc.getPetList().values().iterator();
          while (iterator.hasNext()) {
            L1NpcInstance petNpc = iterator.next();
            L1Location loc = pc.getLocation().randomLocation(3, false);
            int nx = loc.getX();
            int ny = loc.getY();
            if (pc.getMapId() == 5125 || pc.getMapId() == 5131 || pc.getMapId() == 5132 || 
              pc.getMapId() == 5133 || pc.getMapId() == 5134) {
              nx = 32799 + _random.nextInt(5) - 3;
              ny = 32864 + _random.nextInt(5) - 3;
            } 
            teleport(petNpc, nx, ny, mapId, head);
            pet.call_New();
            if (petNpc instanceof L1SummonInstance) {
              L1SummonInstance summon = (L1SummonInstance)petNpc;
              pc.sendPackets((ServerBasePacket)new S_NPCPack_Summon(summon, pc));
            } else if (petNpc instanceof L1PetInstance) {
              L1PetInstance pet2 = (L1PetInstance)petNpc;
              pc.sendPackets((ServerBasePacket)new S_NPCPack_Pet(pet2, pc));
            } 
            Iterator<L1PcInstance> iterator2 = World.get().getVisiblePlayer((L1Object)petNpc).iterator();
            while (iterator2.hasNext()) {
              L1PcInstance visiblePc = iterator2.next();
              visiblePc.removeKnownObject((L1Object)petNpc);
            } 
          } 
          pet.call_New();
        } 
        i++;
      } 
    } 
  }
  
  private static void teleport(L1NpcInstance npc, int x, int y, short map, int head) {
    try {
      World.get().moveVisibleObject((L1Object)npc, map);
      L1WorldMap.get().getMap(npc.getMapId()).setPassable(npc.getX(), npc.getY(), true);
      npc.setX(x);
      npc.setY(y);
      npc.setMap(map);
      npc.setHeading(head);
      L1WorldMap.get().getMap(npc.getMapId()).setPassable(npc.getX(), npc.getY(), false);
    } catch (Exception exception) {}
  }
}
